import ScreenPositionUtil from "./ScreenPositionUtil";
import GameConfig from "./GameConfig";
import DrawUtil from "./DrawUtil";

var countImg = 0;
/**
 * 网格区域
 */
export default class ContentPic extends Laya.Sprite {
    constructor() {
        super();

        this.contentPicData = {};
        this.tag = "";
        this.tStartInAreaIndex = 0;
        this.parentAreaView = null;

        this.currentScaleX = 1; // 图片宽高格
        this.currentScaleY = 1;

        this.currentImageGridList = []; // 图片序号列表

        this.imgPicView = new Laya.Sprite;
        this.addChild(this.imgPicView)

        this.boundsRoot = new Laya.Sprite;
        this.addChild(this.boundsRoot)

        this.removePic = new Laya.Sprite;
        this.addChild(this.removePic)
        this.scalePic = new Laya.Sprite;
        this.addChild(this.scalePic)

        this.imgPicView.on(Laya.Event.MOUSE_DOWN, this, this.onContentPicStartDrag);
        this.imgPicView.on(Laya.Event.MOUSE_UP, this, this.onContentPicEndDrag);

        this.tId = "pic-" + countImg;
        countImg++;

        this.isDraging = false;
    }

    initByContentPicData(startInAreaIndex, inData, inParentAreaView) {
        this.tStartInAreaIndex = startInAreaIndex;
        this.parentAreaView = inParentAreaView;
        this.contentPicData = inData;
        this.currentScaleX = inData.imageGridHSum;
        this.currentScaleY = inData.imageGridVSum;
        this.currentImageGridList = JSON.parse(JSON.stringify(inData.imageGridList));

        this.updatePosByMove();
    }

    initBounds() {
        this.boundsRoot.removeChildren(0,100)
        for (var j = 0; j < this.currentImageGridList.length; j++) {
            let gridindex = this.currentImageGridList[j];

            let gridXIndex = gridindex % 10;
            let gridYIndex = Math.floor(gridindex / 10);

            // left
            if (gridXIndex == 0 || this.currentImageGridList.indexOf(gridindex - 1) == -1)
                this.boundsRoot.addChild(DrawUtil.addBorderLine(j, GameConfig.GridWidth * gridXIndex + GameConfig.elementPadding,
                    GameConfig.GridWidth * gridYIndex + GameConfig.elementPadding, 1, GameConfig.GridWidth - GameConfig.elementPadding * 2,
                    "#555555"));

            // top
            if (gridYIndex == 0 || this.currentImageGridList.indexOf(gridindex - 10) == -1)
                this.boundsRoot.addChild(DrawUtil.addBorderLine(j, GameConfig.GridWidth * gridXIndex + GameConfig.elementPadding,
                    GameConfig.GridWidth * gridYIndex + GameConfig.elementPadding, GameConfig.GridWidth - GameConfig.elementPadding * 2, 1,
                    "#555555"));

            // right
            if (this.currentImageGridList.indexOf(gridindex + 1) == -1)
                this.boundsRoot.addChild(DrawUtil.addBorderLine(j, GameConfig.GridWidth * (gridXIndex + 1) - GameConfig.elementPadding,
                    GameConfig.GridWidth * gridYIndex + GameConfig.elementPadding, 1, GameConfig.GridWidth - GameConfig.elementPadding * 2,
                    "#555555"));

            // bottom
            if (this.currentImageGridList.indexOf(gridindex + 10) == -1)
                this.boundsRoot.addChild(DrawUtil.addBorderLine(j, GameConfig.GridWidth * (gridXIndex + 0) + GameConfig.elementPadding,
                    GameConfig.GridWidth * (gridYIndex + 1) - GameConfig.elementPadding, GameConfig.GridWidth - GameConfig.elementPadding * 2, 1,
                    "#555555"));
        }

    }

    initBtns() {
        this.removePic.size(GameConfig.GridWidth * 0.2, GameConfig.GridWidth * 0.2);
        this.removePic.loadImage("https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/starbucks2/remove.png");

        let gridindex = this.currentImageGridList[0];
        let gridXIndex = gridindex % 10;
        let gridYIndex = Math.floor(gridindex / 10);

        this.removePic.pos(GameConfig.GridWidth * gridXIndex, GameConfig.GridWidth * gridYIndex);
        this.removePic.on(Laya.Event.CLICK, this, this.onRemoveClick);

        this.scalePic.size(GameConfig.GridWidth * 0.2, GameConfig.GridWidth * 0.2);
        this.scalePic.loadImage("https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/starbucks2/scale.png");

        let gridEndindex = this.currentImageGridList[this.currentImageGridList.length - 1];
        let gridEndXIndex = gridEndindex % 10;
        let gridEndYIndex = Math.floor(gridEndindex / 10);

        this.scalePic.pos(GameConfig.GridWidth * (gridEndXIndex + 1) - this.scalePic.width, GameConfig.GridWidth * (gridEndYIndex + 1) - this.scalePic.height);

        this.scalePic.on(Laya.Event.MOUSE_DOWN, this, this.onScaleStartDrag);
        this.scalePic.on(Laya.Event.MOUSE_UP, this, this.onScaleEndDrag);
        this.scalePic.on(Laya.Event.MOUSE_MOVE, this, this.onScaleMoveDrag);
    }

    onRemoveClick() {
        if (this.parentAreaView)
            this.parentAreaView.removeContentPic(this);
    }

    onContentPicStartDrag() {
        console.log("onContentPicStartDrag")

        this.startDrag(null, false, 50);
        this.setBoundsAndBtnsVisible(true,true,true);
        this.parentAreaView.updateSelectState(this , true);
        this.isDraging = true;
        this.zOrder = 100;
    }

    onContentPicEndDrag() {
        if(!this.isDraging)
            return;
        
        console.log("onContentPicEndDrag")
        if (this.parentAreaView)
            this.parentAreaView.endDragContentPic(this.x, this.y, this);
        this.setBoundsAndBtnsVisible(true,true,true);

        this.isDraging = false;
        this.zOrder = 1;
    }

    onScaleStartDrag() {
        console.log("onScaleStartDrag")
        this.scalePic.startDrag(null, false, 50);
        this.setBoundsAndBtnsVisible(true,true,true);
        this.parentAreaView.updateSelectState(this , true);
    }

    onScaleEndDrag(e) {
        console.log("onScaleEndDrag")
        // if (this.parentAreaView)
        //     this.parentAreaView.endDragScaleDrag(this.x ,this.y ,this);
        this.tryUpdatePicsEndScale(e.stageX, e.stageY)
        this.setBoundsAndBtnsVisible(true,true,true);
    }

    onScaleMoveDrag(e) {
        console.log("onScaleMoveDrag")
        console.log(e)

        let areaPosPoint = this.globalToLocal(new Laya.Point(e.stageX, e.stageY))
        let pointX = areaPosPoint.x;
        let pointY = areaPosPoint.y;

        this.updatePicDuringScale(pointX, pointY);
        this.setBoundsAndBtnsVisible(false , false,true);
    }

    /**
     * 移动后更新位置 
     * @param {*} startInAreaYIndex 
     */
    updatePosByMove(inStartInAreaYIndex = -1) {
        if(inStartInAreaYIndex != -1)
            this.tStartInAreaIndex = inStartInAreaYIndex;

        // pic在区域内起始位置的横纵分量
        let startInAreaXIndex = this.tStartInAreaIndex % 10;
        let startInAreaYIndex = Math.floor(this.tStartInAreaIndex / 10);
        this.pos(startInAreaXIndex * GameConfig.GridWidth, startInAreaYIndex * GameConfig.GridWidth);

        this.imgPicView.size(this.currentScaleX * GameConfig.GridWidth - (GameConfig.GridWidth * 0.3),
            this.currentScaleY * GameConfig.GridWidth - (GameConfig.GridWidth * 0.3));

        this.imgPicView.pos(GameConfig.GridWidth * 0.15, GameConfig.GridWidth * 0.15);
        this.imgPicView.loadImage(this.contentPicData.imageUrl);

        this.initBounds();
        this.initBtns();
        this.setBoundsAndBtnsVisible(false,false,false);
    }

    updatePicDuringScale(inX, inY) {
        let tempwidth = inX - this.imgPicView.x;
        let tempheight = inY - this.imgPicView.y;
        this.imgPicView.size(tempwidth, tempheight)
    }

    tryUpdatePicsEndScale(inX, inY) {
        // 当前宽高
        let tempwidth = inX - this.imgPicView.x;
        let tempheight = inY - this.imgPicView.y;

        // 当前宽高占用的格子数
        let currentGridWidthSum = Math.round(this.imgPicView.width / GameConfig.GridWidth);
        let currentGridHeightSum = Math.round(this.imgPicView.height / GameConfig.GridWidth);

        // 原始宽高占用的格子数
        let originWidthSum = this.contentPicData.imageGridHSum;
        let originHeightSum = this.contentPicData.imageGridVSum;

        // 当前 - 原始   格子数差
        let gapWidth = currentGridWidthSum - originWidthSum;
        let gapHeight = currentGridHeightSum - originHeightSum;

        // 最小差
        let minGap = Math.min(gapWidth, gapHeight);
        let targetWidthGap, targetHeightGap;

        // 计算缩放后的倍数是否为原始的整数倍
        if (minGap == gapWidth) {
            targetWidthGap = gapWidth;
            targetHeightGap = (gapWidth * originHeightSum) / originWidthSum;
        } else // if (minGap == gapHeight) 
        {
            targetHeightGap = gapHeight;
            targetWidthGap = (gapHeight * originWidthSum) / originHeightSum;
        }

        // 等比放大
        if (Number.isInteger(targetWidthGap) && Number.isInteger(targetHeightGap)) {
            // 整数倍缩放
            currentGridWidthSum = originWidthSum + targetWidthGap;
            currentGridHeightSum = originHeightSum + targetHeightGap;
        } else {
            // 恢复拖拽缩放之前
            currentGridWidthSum = this.currentScaleX;
            currentGridHeightSum = this.currentScaleY;
        }

        if (currentGridWidthSum <= 0 || currentGridHeightSum <= 0) {
            currentGridWidthSum = this.currentScaleX;
            currentGridHeightSum = this.currentScaleY;
          }

        // 计算目标图片占用格序列
        let widthSum = currentGridWidthSum;
        let heightSum = currentGridHeightSum;
        let tempPicGridList = [];
        for (var i = 0; i < heightSum; i++) {
            for (var j = 0; j < widthSum; j++) {
                tempPicGridList.push((i * 10) + j);
            }
        }

        let temppos = this.parentAreaView.checkIsHaveEnoughGapInAreaPos(this.tStartInAreaIndex, tempPicGridList, this);
        console.log("temppos : " + temppos + " - " + tempPicGridList.toString())
        if (temppos != -1) {
            this.currentScaleX = widthSum;
            this.currentScaleY = heightSum;
            this.currentImageGridList = tempPicGridList;
            this.updatePosByMove();
        }

    }

    setBoundsAndBtnsVisible(bv,rv,sv) {
        this.boundsRoot.visible = bv;
        this.removePic.visible = rv;
        this.scalePic.visible = sv;
    }
}

